/* Creado el  21/04/2006 */
package com.angel.arquitectura.service.impl;

import com.angel.arquitectura.context.ExecutionContext;
import com.angel.arquitectura.dao.factory.ArquitecturaDAOFactory;
import com.angel.arquitectura.dto.security.SessionUserDTO;
import com.angel.arquitectura.model.common.PersistentObject;
import com.angel.arquitectura.service.GenericService;
import com.angel.dao.generic.factory.DAOFactory;
import com.angel.dao.generic.interfaces.GenericDAO;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.Collection;

/**
 * Clase que brinda utilidades para todas las implementaciones de los servicios de la aplicacion.
 * 
 * @author ALeinvand
 * @version $Revision: 1.3.20.9 $
 */
public abstract class GenericServiceImpl implements GenericService{
    
    /**
     *  Esta variable permite que cada service tenga su dao correspondiente. el problema es que hay que 
     * realizar un casteo para adquirir la funcionalidad especifica del dao que se desea, lo que se puede
     * hacer sino es sobreescribir el metodo y realizar el casteo dentro del mismo 
     */
    private GenericDAO genericDAO;

    private DAOFactory daoFactory;

    @Autowired
    private ArquitecturaDAOFactory arquitecturaDAOFactory;

    public GenericServiceImpl(DAOFactory daoFactory){
        super();
        this.daoFactory = daoFactory;
    }

    public GenericServiceImpl(){
        super();
    }
    
    public SessionUserDTO getSessionUserDTO(){
		return ExecutionContext.getContext().getSessionUserDTO();
	}

    public GenericDAO getGenericDAO() {
        return genericDAO;
    }

    public void setGenericDAO(GenericDAO dao){
        this.genericDAO = dao;
    }

    @SuppressWarnings({"unchecked"})
    public Object findUniqueByID(Serializable id) {
        return this.getGenericDAO().findUniqueByCode(id);
    }

    @SuppressWarnings({"unchecked"})
    //@Secured(rol = {"admin"})
    public Collection<Object> findAll() {
        Long count = this.getGenericDAO().count();
        Collection<Object> entities = this.getGenericDAO().findAll();
        return entities;
    }

    @SuppressWarnings({"unchecked"})
    //public void create(BusinessObject businessObject) {
    public PersistentObject create(PersistentObject persistentObject) {
        return (PersistentObject) this.getGenericDAO().persist(persistentObject);
    }

    @SuppressWarnings({"unchecked"})
    public void remove(Object persistentObject) {
        this.getGenericDAO().delete(persistentObject);
    }

    @SuppressWarnings({"unchecked"})
    public Object update(Object persistentObject) {
        Object object = persistentObject;
        this.getGenericDAO().update(object);
        return persistentObject;
    }

    public DAOFactory getDaoFactory() {
        return daoFactory;
    }

    public void setDaoFactory(DAOFactory daoFactory) {
        this.daoFactory = daoFactory;
    }

    @SuppressWarnings({"unchecked"})
    protected < T extends GenericDAO> T getGenericDAO(Class<T> genericDAO){
        return (T) this.getGenericDAO();
    }

    public ArquitecturaDAOFactory getArquitecturaDAOFactory() {
        return arquitecturaDAOFactory;
    }

    public void setArquitecturaDAOFactory(ArquitecturaDAOFactory arquitecturaDAOFactory) {
        this.arquitecturaDAOFactory = arquitecturaDAOFactory;
    }
}